<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>生成地图缓存</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="http://cdn.bootcss.com/leaflet/1.0.3/leaflet.css">
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="model" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body">
                <p>正在创建生成地图缓存...</p>
            </div>
        </div>
    </div>
</div>
<div id="map" style="width: 100%;height:100%"></div>

<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://cdn.bootcss.com/leaflet/1.0.3/leaflet.js"></script>
<script type="text/javascript" src="../../dist/iclient9-leaflet.min.js"></script>
<script type="text/javascript">
    var layer, processingUrl = "http://117.122.248.69:8090/iserver/services/spatialprocessing/rest/v1/jobs",
            token = 'oZdJwpVhYnPNEAt8-ykB-Cy39gEZkdvPZ9knTbCdui1SoBeXWVCwx-j5tOVSScotsUX1qwZF44X9m6wNME1kBg..',
            map = L.map('map', {
                crs: L.CRS.TianDiTu_WGS84,
                center: [40.75, -73.95],
                maxZoom: 18,
                zoom: 12
            }),
            info = L.control({position: 'topright'});
    L.supermap.tiandituTileLayer("http://t{s}.tianditu.com/vec_c/wmts", {
        layer: "vec",
        tilematrixSet: "c"
    }).addTo(map);
    info.onAdd = function () {
        var popup = L.DomUtil.create('div');
        popup.style = 'width:22%';
        popup.innerHTML = "<div class='panel panel-default'>" +
                "<div class='panel-heading'>" +
                "<h3 class='panel-title'>地图缓存</h3>" +
                "</div>" +
                "<div class='panel-body'>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>输入数据<span title='必填字段' style='color: red;'> * </span>　　　</span>" +
                "<div>" +
                "<select class='form-control' id='datasetName' name='clientType'>" +
                "<option selected='selected'>newyork_taxi_2013_01_14k_csv_newyork_taxi_2013-01_14k</option>" +
                "</select> </div> </div> <p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>缓存类型<span title='必填字段' style='color: red;'> * </span>　　　</span>" +
                "<div>" +
                "<select class='form-control' id='imageType' name='clientType'>" +
                "<option value='Heatmap' selected='selected'>热度图</option>" +
                "</select> </div> </div> <p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>缓存范围<span title='必填字段' style='color: red;'> * </span>　　　</span>" +
                "<input id='bounds' type='text' class='form-control' value='-74.050, 40.650, -73.850, 40.850'/>" +
                "</div> <p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>缓存比例尺级别<span title='必填字段' style='color: red;'> * </span></span>" +
                "<input id='level' type='text' class='form-control' value='5'/>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>缓存名称<span title='必填字段' style='color: red;'> * </span>　　　</span>" +
                "<input id='cacheName' type='text' class='form-control' value='cache'/>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>存储类型<span title='必填字段' style='color: red;'> * </span>　　　</span>" +
                "<div>" +
                "<select class='form-control' id='cacheType' name='clientType'>" +
                "<option selected='selected'>MongoDB</option>" +
                "</select></div></div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>服务地址<span title='必填字段' style='color: red;'> * </span>　　　</span>" +
                "<input id='serverAdresses' type='text' class='form-control' value='117.122.248.69:27017'/>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>数据库<span title='必填字段' style='color: red;'> * </span>　　　　</span>" +
                "<input id='database' type='text' class='form-control' value='smtiles'/>" +
                "</div><p>" +
                "<div class='input-group'>" +
                "<span class='input-group-addon'>版本<span title='必填字段' style='color: red;'> * </span>　　　　　</span>" +
                "<input id='version' type='text' class='form-control' value='v1'/>" +
                "</div><p>" +
                "<div align='right'>" +
                "<input type='button' id='btn' class='btn btn-primary' value='创建'/>" +
                "</div></div></div>";
        handleMapEvent(popup, this._map);
        return popup;
    };
    info.addTo(map);

    function getBounds() {
        var bounds = [];
        $('#bounds').val().split(',').map(function (el) {
            bounds.push(parseFloat(el));
        });
        return bounds;
    }
    function getServerAddress() {
        var serverAddress = [];
        serverAddress.push($('#serverAdresses').val().toString());
        return serverAddress;
    }
    var processingService = new L.supermap.processingService(processingUrl);
    SuperMap.SecurityManager.registerToken("http://117.122.248.69:8090", token);

    $('#btn').on('click', function () {
        $('#btn').prop('disabled', true);
        $("#model").modal({show: true, backdrop: 'static', keyboard: false});
        if (map && layer) {
            map.removeLayer(layer);
        }
        var buildCacheJobParameter = new SuperMap.BuildCacheJobParameter({
            datasetName: $('#datasetName').val(),
            cacheName: $('#cacheName').val(),
            cacheType: $('#cacheType option:selected').text(),
            serverAdresses: getServerAddress(),
            database: $('#database').val(),
            version: $('#version').val(),
            bounds: L.latLngBounds(L.latLng(getBounds()[1], getBounds()[0]), L.latLng(getBounds()[3], getBounds()[2])),
            imageType: $('#imageType option:selected').attr('value'),
            level: parseInt($('#level').val())
        });
        processingService.addBuildCacheJob(buildCacheJobParameter, function (serviceResult) {
            if (serviceResult.error) {
                $('#btn').prop('disabled', false);
                $("#model").modal('hide');
                var errorMsg = serviceResult.error.errorMsg || "code: " + serviceResult.error.code;
                showAlert("创建失败!<br>" + errorMsg, false);
                return;
            }
            serviceResult.result.setting.serviceInfo.targetServiceInfos.map(function (info) {
                if (info.serviceType === 'RESTMAP') {
                    SuperMap.FetchRequest.get(info.serviceAddress + '/maps').then(function (response) {
                        return response.json();
                    }).then(function (result) {
                        var mapUrl = result[0].path;
                        layer = L.supermap.imageMapLayer(mapUrl, {noWrap: true, transparent: true});
                        layer.addTo(map);
                        $("#model").modal('hide');
                        $('#btn').prop('disabled', false);
                    });
                }
            });
        });
    });

    function showAlert(msg, state) {
        var className = "alert-";
        className += state ? "success" : "danger";
        if (!$('#msg_container')[0]) {
            var alertDiv = $("<div class='alert alert-dismissible' id='msg_container' role='alert' " +
                    "style='z-index:800;position: absolute;top: 20px;margin:auto;left: 40%;width:400px;display: none'>" +
                    "<button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button>" +
                    "<strong><p id='msg' style='word-wrap: break-word'></p></strong></div>");
            $('body').append(alertDiv)
        }
        $('#msg_container').addClass(className);
        $('#msg_container').slideDown(300);
        $('#msg').html(msg);
    }

    function handleMapEvent(div, map) {
        if (!div || !map) {
            return;
        }
        div.addEventListener('mouseover', function () {
            map.dragging.disable();
            map.scrollWheelZoom.disable();
            map.doubleClickZoom.disable();
        });
        div.addEventListener('mouseout', function () {
            map.dragging.enable();
            map.scrollWheelZoom.enable();
            map.doubleClickZoom.enable();
        });
    }

</script>
</body>
</html>